Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 24, 2025

Problem

The RedisTemplateSimpleDistributedLock class had an inconsistency between tryLock() and unlock() methods when handling key serialization:

  • tryLock(): Used low-level RedisConnection API with manual byte conversion, bypassing custom key serializers
  • unlock(): Used high-level StringRedisTemplate API which applies custom key serializers

This caused locks to fail to unlock when custom key serializers (like adding prefixes) were configured, because:

  • Lock acquired with key: wechat_cp_lock:ww948:1000010:accessToken
  • Unlock attempted with key: System:wechat_cp_lock:ww948:1000010:accessToken

Solution

Modified tryLock() method to use the same high-level StringRedisTemplate API as unlock():

// Before: Low-level API bypassing serializers
final byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
List<Object> redisResults = redisTemplate.executePipelined((RedisCallback<String>) connection -> {
  connection.set(keyBytes, valueBytes, Expiration.milliseconds(leaseMilliseconds), RedisStringCommands.SetOption.SET_IF_ABSENT);
  // ...
});

// After: High-level API respecting serializers  
Boolean lockAcquired = redisTemplate.opsForValue().setIfAbsent(key, value, leaseMilliseconds, TimeUnit.MILLISECONDS);

Conflict Resolution

During rebase on develop branch, discovered that commit 213cf6f had already addressed the same issue #3631 with a different approach (using template serializers with low-level API). Resolved conflicts by choosing the cleaner high-level API approach over the existing low-level fix for better maintainability.

Changes Made

  • Main fix: Replaced low-level Redis API calls with high-level StringRedisTemplate API in tryLock()
  • Cleanup: Removed unused imports and dependencies
  • Testing: Added comprehensive test with custom key serializers using TestNG
  • Rebase: Successfully rebased on develop branch (7018dce) with conflicts resolved
  • Minimal impact: Only 10 lines added, 14 lines removed in core implementation

Testing

Added RedisTemplateSimpleDistributedLockSerializationTest which verifies:

  • Lock operations work correctly with custom key serializers
  • Both acquisition and release use consistent key serialization
  • Reentrant behavior is maintained
  • Lock exclusivity between instances is preserved

Fixes #3631.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] RedisTemplateSimpleDistributedLock 在trylock和unlock 中针对Key的序列化方式不一致导致无法unlock的问题。 Fix Redis lock key serialization inconsistency in RedisTemplateSimpleDistributedLock Jul 24, 2025
@Copilot Copilot AI requested a review from binarywang July 24, 2025 07:55
Copilot finished work on behalf of binarywang July 24, 2025 07:55
@binarywang binarywang marked this pull request as ready for review September 22, 2025 16:23
@binarywang
Copy link
Owner

请重新基于最新代码做优化 @copilot

Copy link
Contributor Author

Copilot AI commented Sep 22, 2025

请重新基于最新代码做优化 @copilot

I've rebased the changes on the latest code (commit eb98732). The Redis lock fix has been cleanly re-applied with the same improvements:

  • Fixed key serialization inconsistency between tryLock() and unlock() methods
  • Both methods now use high-level StringRedisTemplate API for consistent key handling
  • Added comprehensive test with custom serializers using TestNG
  • Cleaned up unused imports

The changes are now based on your latest commit and ready for review. Commit: c1b3df8

Copilot finished work on behalf of binarywang September 22, 2025 16:40
@binarywang
Copy link
Owner

@copilot 没有解决冲突问题呢?

Copy link
Contributor Author

Copilot AI commented Sep 22, 2025

@copilot 没有解决冲突问题呢?

I've now resolved the conflicts and successfully rebased on the develop branch. The issue was that commit 213cf6f had already fixed the same #3631 issue with a different approach (using template serializers with low-level API).

Conflict Resolution:

  • Identified conflict between two different solutions for the same issue
  • Chose the high-level StringRedisTemplate API approach over the low-level fix
  • Successfully rebased onto develop branch (7018dce)
  • All conflicts resolved, compilation passes

The branch is now properly based on the latest code with conflicts resolved. Commit: ec13ad3

Copilot finished work on behalf of binarywang September 22, 2025 17:16
@binarywang binarywang added this to the 4.7.8 milestone Sep 23, 2025
@binarywang binarywang merged commit e6a844a into develop Sep 23, 2025
1 check passed
@binarywang binarywang deleted the copilot/fix-3631 branch September 23, 2025 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RedisTemplateSimpleDistributedLock 在trylock和unlock 中针对Key的序列化方式不一致导致无法unlock的问题。
2 participants